package com.probiz.estore.common.model.cart;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.cart.base.ShoppingcartItemTbl;
import com.probiz.estore.common.model.product.ProductSku;
import com.probiz.estore.common.model.product.ProductType;
import com.probiz.estore.common.model.product.WholesalePrice;
import com.probiz.estore.common.model.sales.Gift;

/**
 * Model - Business object
 * This file won't get overwritten.
 */
public class ShoppingcartItem extends ShoppingcartItemTbl {
	
	public static final short ISONSALE_YES = 1;
	public static final short ISONSALE_NO = 0;
	public static final short ISWHOLESALE_YES = 1;
	public static final short ISWHOLESALE_NO = 0;
	//是否有折扣
    private boolean hasDiscount = false;
    //税名称
    private String taxName;
    //税
    private BigDecimal tax = new BigDecimal(0);
    //折扣数量
    private Integer discountQuantity = 0;
    //物品小计
    private BigDecimal total;
    
    /*--start:用于保存促销引擎计价临时变量----*/
    private BigDecimal discountPriceRAM = new BigDecimal(0);
    private Integer discountQuantityRAM = 0;
    private boolean catalogConditonFlagRAM = false;
    private boolean catalogConditionRunInAllFlagRAM = true;
    private boolean catalogConditionRunInAnyFlagRAM = false;
    private boolean isCatalogConditionGiftFlagRAM = false;
    /*--end:用于保存促销引擎计价临时变量----*/
       
  	/**
	 * Default Empty Constructor for class ShoppingcartItem
	 */
	public ShoppingcartItem () {
		super();
	}
	
	/**
	 * Default Key Fields Constructor for class ShoppingcartItem
	 */
	public ShoppingcartItem (
		 Integer in_shoppingcartItemId
		) {
		super (
		  in_shoppingcartItemId
		);
	}
	
	public ShoppingcartItem (ProductSku productsku, Integer quantity) {
		super();
		setProductSku(productsku);
		setAddTime(new Date());
		setCreateTime(new Date());
		setPrice(productsku.getPrice());
		setSkuType(productsku.getProduct().getProductType().getKind());  
		setQuantity(quantity);
		setItemType(Constants.ITEM_TYPE_PRODUCT);
		
		this.calculateSalesAndWholesalePrice();
	}
	
	public ShoppingcartItem(ShoppingcartItemGc shoppingcartItemGc) {
		super();
		setShoppingcartItemGc(shoppingcartItemGc);
		setAddTime(new Date());
		setCreateTime(new Date());
		setPrice(shoppingcartItemGc.getGiftCertAmt());
		setDiscountPrice(shoppingcartItemGc.getGiftCertAmt());
		setTotal(shoppingcartItemGc.getGiftCertAmt());
		setSkuType((short)0);  
		setQuantity(1);
		setItemType(Constants.ITEM_TYPE_GC);
		setIsOnSale(ShoppingcartItem.ISONSALE_NO);
		setIsWholesale(ShoppingcartItem.ISWHOLESALE_NO);
		shoppingcartItemGc.setShoppingcartItem(this);
	}
	
    public ShoppingcartItem (Gift gift, Integer quantity) {
		super();
		this.setGift(gift);
		this.setAddTime(new Date());
		this.setCreateTime(new Date());
		this.setPrice(gift.getPrice());
		this.setSkuType(gift.getKind());  
		this.setQuantity(quantity);
		this.setItemType(Constants.ITEM_TYPE_GIFT);
	}
	
    
    public Integer getDiscountQuantity() {
		return this.discountQuantity;
	}
	
	
	public void setDiscountQuantity(Integer aValue) {
		this.discountQuantity = aValue;
	}
	
	public BigDecimal getDiscountPriceRAM() {
		return discountPriceRAM;
	}
	public void setDiscountPriceRAM(BigDecimal discountPriceRAM) {
		this.discountPriceRAM = discountPriceRAM;
	}
	public Integer getDiscountQuantityRAM() {
		return discountQuantityRAM;
	}
	public void setDiscountQuantityRAM(Integer discountQuantityRAM) {
		this.discountQuantityRAM = discountQuantityRAM;
	}
	
	
	public boolean isCatalogConditonFlagRAM() {
		return catalogConditonFlagRAM;
	}
	public void setCatalogConditonFlagRAM(boolean catalogConditonFlagRAM) {
		this.catalogConditonFlagRAM = catalogConditonFlagRAM;
	}
	public boolean isCatalogConditionRunInAllFlagRAM() {
		return catalogConditionRunInAllFlagRAM;
	}
	public void setCatalogConditionRunInAllFlagRAM(
			boolean catalogConditionRunInAllFlagRAM) {
		this.catalogConditionRunInAllFlagRAM = catalogConditionRunInAllFlagRAM;
	}
	public boolean isCatalogConditionRunInAnyFlagRAM() {
		return catalogConditionRunInAnyFlagRAM;
	}
	public void setCatalogConditionRunInAnyFlagRAM(
			boolean catalogConditionRunInAnyFlagRAM) {
		this.catalogConditionRunInAnyFlagRAM = catalogConditionRunInAnyFlagRAM;
	}
	
	public boolean isCatalogConditionGiftFlagRAM() {
		return isCatalogConditionGiftFlagRAM;
	}
	/**
	 *  1:product (default)
		2:gift certificate
		3:service
		4:software
		5:document
		6:music
		7:video
		8:others
	 * @return
	 */
	public String getShoppingcartItemTypeName(){
		switch(this.skuType){  
			case 2:
				return "Gift Certificate";
			case 3:
				return "Serivce";
			case 4:
				return "Software";
			case 5:
				return "Document";
			case 6:
				return "Music";
			case 7:
				return "Video";
			case 8:
				return "Others";
		    default:
		    	return "Product";
					
		}
	}
	public String getTaxName() {
		return taxName;
	}
	public void setTaxName(String taxName) {
		this.taxName = taxName;
	}
	public BigDecimal getTax() {
		return tax;
	}
	public void setTax(BigDecimal tax) {
		this.tax = tax;
	}
	public BigDecimal getTotal() {
		return total;
	}
	public void setTotal(BigDecimal total) {
		this.total = total;
	}
	 
    public boolean isHasDiscount()
    {
        return hasDiscount;
    }
    public void setHasDiscount(boolean hasDiscount)
    {
        this.hasDiscount = hasDiscount;
    }	
    
	public void plusQuantity(Integer quantity) {
		this.quantity += quantity;
		this.calculateSalesAndWholesalePrice();
	}
	
	public void calculateSalesAndWholesalePrice() {
		if (!this.isProduct()) {
			//礼品券和积分换礼品不参与批发价计算
			return;
		}
		
		ProductSku sku = getProductSku();
		if (sku.getSalePrice() != null && sku.getSalePrice().doubleValue() != 0) {
			setIsOnSale(Constants.FLAG_TRUE);
			setDiscountPrice(sku.getSalePrice());
		} else {
			setIsOnSale(Constants.FLAG_FALSE);
		}
		if (Constants.FLAG_TRUE.equals(getIsOnSale()))
			return; // 满足特价的商品，不参与批发价

		Set<WholesalePrice> wholesalePrice = sku.getWholesalePrices();
		// 找出最满足条件的批发的价，当满足批发条件，这该item不参与促销活动
		SortedSet<WholesalePrice> sortPrice = new TreeSet<WholesalePrice>(
				new Comparator<WholesalePrice>() {
					public int compare(WholesalePrice o1, WholesalePrice o2) {
						int q1 = (o1.getMinQuantity() == null)? 0: o1.getMinQuantity();
						int q2 = (o2.getMinQuantity() == null)? 0: o2.getMinQuantity();
						if (q1 < q2)
							return -1;
						else if (q1 == q2)
							return 0;
						else
							return 1;
					}
				});
		sortPrice.addAll(wholesalePrice);
		WholesalePrice w1 = new WholesalePrice();
		w1.setMinQuantity(quantity);
		if (sortPrice.contains(w1)) {
			setIsWholesale(Constants.FLAG_TRUE);
			for (WholesalePrice p : wholesalePrice) {
				if (p.getMinQuantity().intValue() == quantity) {
					setDiscountPrice(p.getPrice());
					break;
				}
			}
		} else {
			WholesalePrice w2 = new WholesalePrice();
			w2.setMinQuantity(0);
			if (sortPrice.subSet(w2, w1).size() != 0) {
				WholesalePrice who = sortPrice.subSet(w2, w1).last();
				setIsWholesale(Constants.FLAG_TRUE);
				setDiscountPrice(who.getPrice());
			} else {
				setIsWholesale(Constants.FLAG_FALSE);
				setDiscountPrice(sku.getPrice());
			}
		}
	}
	
	//计算订单项商品的重量
	public BigDecimal getWeight() {
		BigDecimal weight = productSku.getWeight();
		return (weight == null)? (new BigDecimal(0)): weight.multiply(BigDecimal.valueOf(quantity));
	}
	
	//该订单商品项是否礼品券
	public boolean isGiftCertificate() {
		return Constants.ITEM_TYPE_GC.equals(itemType);
	}
	
	//该订单商品项是否普通商品
	public boolean isProduct() {
		return Constants.ITEM_TYPE_PRODUCT.equals(itemType);
	}
	
	//该订单商品项是否实体商品(首先它是一个普通的商品而不是礼品券，其次它的商品类型属性是"实体商品")
	public boolean isPhysicalProduct() {
		if (isProduct()) {
			if (ProductType.PRODUCT_KIND_ENTITY.equals(skuType)) {
				return true;
			}
		}
		
		return false;
	}
	
	//该订单商品项是否数字商品(首先它是一个普通的商品而不是礼品券，其次它的商品类型属性是"服务"、"下载"或"卡类产品")
	public boolean isDigitalProduct() {
		if (isProduct()) {
			if (! ProductType.PRODUCT_KIND_ENTITY.equals(skuType)) {
				return true;
			}
		}
		
		return false;
	}
	
	//该订单礼品项是否普通商品
	public boolean is_Gift() {
		return Constants.ITEM_TYPE_GIFT.equals(itemType);
	}
	
	@Override
	public int hashCode() {
		return super.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}
	
	public Integer getHashCode() {
		return super.hashCode();
	}
	
	
	
}
